package sg.edu.nus.comp.icreate.ivletalk.voice;

import java.util.List;

import sg.edu.nus.comp.icreate.ivletalk.BaseActivity;
import sg.edu.nus.comp.icreate.ivletalk.module.MainActivity;
import android.content.Context;

public class CommonCommandManager {
	public final String SPACE = " ";

	public boolean executeMenuCommand(String cmd, Context context) {
		if (cmd.contains(VoiceCommand.BACK.getPattern()) || cmd.contains(VoiceCommand.RETURN.getPattern())) {
			((BaseActivity) context).onBackPressed();
			return true;
		} else if (cmd.contains(VoiceCommand.EVENTS.getPattern())) {
			((BaseActivity) context).executeMenuVoiceCommand(VoiceCommand.EVENTS.getPattern());
			return true;
		} else if (cmd.contains(VoiceCommand.HELP.getPattern())) {
			((BaseActivity) context).executeMenuVoiceCommand(VoiceCommand.HELP.getPattern());
			return true;
		} else if (cmd.contains(VoiceCommand.MODULES.getPattern()) || cmd.contains(VoiceCommand.MAIN_PAGE.getPattern())) {
			((BaseActivity) context).executeMenuVoiceCommand(VoiceCommand.MODULES.getPattern());
			return true;
		} else if (cmd.contains(VoiceCommand.REFRESH.getPattern())) {
			((BaseActivity) context).executeMenuVoiceCommand(VoiceCommand.REFRESH.getPattern());
			return true;
		} else if (cmd.contains(VoiceCommand.LOGOUT.getPattern())) {
			((BaseActivity) context).executeMenuVoiceCommand(VoiceCommand.LOGOUT.getPattern());
			return true;
		} else if (cmd.contains(VoiceCommand.NEW.getPattern())) {
			((BaseActivity) context).executeMenuVoiceCommand(VoiceCommand.NEW.getPattern());
			return true;
		} else if (cmd.contains(VoiceCommand.REPLY.getPattern())) {
			((BaseActivity) context).executeMenuVoiceCommand(VoiceCommand.REPLY.getPattern());
			return true;
		} else if (cmd.contains(VoiceCommand.SING_A_SONG.getPattern())) {
			((BaseActivity) context).executeMenuVoiceCommand(VoiceCommand.SING_A_SONG.getPattern());
			return true;
		} else {
			return false;
		}
	}

	public boolean selectItemByNumber(String cmd, Context context) {
		int position = -1;
		if(((BaseActivity) context).getDisplayedItem() == null)
			return false;
		int maxPosition = ((BaseActivity) context).getDisplayedItem().size() - 1;
		if (cmd.contains(VoiceCommand.FIRST.getPattern()) || cmd.contains(VoiceCommand.ONE.getPattern()))
			position = 0;
		else if (cmd.contains(VoiceCommand.THIRD.getPattern()) || cmd.contains(VoiceCommand.THREE.getPattern()))
			position = 2;
		else if (cmd.contains(VoiceCommand.FOURTH.getPattern()) || cmd.contains(VoiceCommand.FOUR.getPattern()))
			position = 3;
		else if (cmd.contains(VoiceCommand.SECOND_LAST.getPattern()))
			position = maxPosition - 1;
		else if (cmd.contains(VoiceCommand.SECOND.getPattern()) || cmd.contains(VoiceCommand.TWO.getPattern()))
			position = 1;
		else if (cmd.equals(VoiceCommand.LAST.getPattern()))
			position = maxPosition;

		if (position >= 0 && position < maxPosition) {
			((BaseActivity) context).selectNumberedItem(position);
			return true;
		} else
			return false;
	}

	public boolean selectItemByName(String cmd, Context context) {
		cmd = VoiceCommandUtil.processString(cmd);
		List<?> itemList = ((BaseActivity) context).getDisplayedItem();
		if (itemList == null)
			return false;
		int position = -1;
		for (Object o : itemList) {
			position++;
			if ((cmd.contains(VoiceCommandUtil.processString(o.toString())) && o.toString().length() > 2)
					|| VoiceCommandUtil.processString(o.toString()).contains(cmd)
					|| VoiceCommandUtil.getSimilarity(cmd, VoiceCommandUtil.processString(o.toString())) > 0.65) {
				((BaseActivity) context).selectNumberedItem(position);
				return true;
			}
		}
		return false;
	}

	public boolean openWebPage(String cmd, Context context) {
		String oCmd = cmd.toLowerCase();
		cmd = VoiceCommandUtil.processString(cmd);
		String url = null;
		if (cmd.contains(VoiceCommand.MAIL.getPattern())) {
			url = WebPageUrl.MAIL.getUrl();
		} else if (cmd.contains(VoiceCommand.GOOGLE.getPattern())) {
			url = WebPageUrl.GOOGLE.getUrl() + oCmd.replace("google", "").replace(" ", "");
		} else if (cmd.contains(VoiceCommand.MAP.getPattern())) {
			url = WebPageUrl.MAP.getUrl();
		} else if (cmd.contains(VoiceCommand.LIBRARY.getPattern())) {
			url = WebPageUrl.LIBRARY.getUrl();
		} else if (cmd.endsWith(VoiceCommand.SOC.getPattern())) {
			url = WebPageUrl.SOC.getUrl();
		} else if (cmd.endsWith(VoiceCommand.FAS.getPattern())) {
			url = WebPageUrl.FAS.getUrl();
		} else if (cmd.equals(VoiceCommand.SCIENCE.getPattern())) {
			url = WebPageUrl.SCIENCE.getUrl();
		} else if (cmd.contains(VoiceCommand.NUS.getPattern())) {
			url = WebPageUrl.NUS.getUrl();
		} else if (cmd.contains(VoiceCommand.BUSINESS_SCHOOL.getPattern())) {
			url = WebPageUrl.BUSINESS_SCHOOL.getUrl();
		} else if (cmd.contains(VoiceCommand.DENTISTRY.getPattern())) {
			url = WebPageUrl.DENTISTRY.getUrl();
		} else if (cmd.contains(VoiceCommand.SDE.getPattern())) {
			url = WebPageUrl.SDE.getUrl();
		} else if (cmd.contains(VoiceCommand.ENGINEERING.getPattern())) {
			url = WebPageUrl.ENGINEERING.getUrl();
		} else if (cmd.contains(VoiceCommand.LAW.getPattern())) {
			url = WebPageUrl.LAW.getUrl();
		} else if (cmd.contains(VoiceCommand.MEDICINE.getPattern())) {
			url = WebPageUrl.MEDICINE.getUrl();
		} else if (cmd.contains(VoiceCommand.MUSIC.getPattern())) {
			url = WebPageUrl.MUSIC.getUrl();
		}

		if (url != null) {
			((BaseActivity) context).openWebPage(url);
			return true;
		}
		return false;
	}

	public boolean chat(String cmd, Context context) {
		cmd = VoiceCommandUtil.processString(cmd);
		String ans = null;
		if (cmd.contains(ChatQuestion.HOW_ARE_YOU.getQuestion())) {
			ans = Chat.HOW_ARE_YOU.getAns();
		} else if (cmd.contains(ChatQuestion.WHO_ARE_YOU.getQuestion())) {
			ans = Chat.WHO_ARE_YOU.getAns();
		} else if (cmd.contains(ChatQuestion.HELLO.getQuestion())) {
			ans = Chat.HELLO.getAns();
		} else if (cmd.contains(ChatQuestion.ON_THE_SCREEN.getQuestion())) {
			((BaseActivity) context).speak(Chat.ON_THE_SCREEN.getAns());
			List<?> itemList = ((BaseActivity) context).getDisplayedItem();
			if (itemList != null && itemList.size() > 0){
				for (Object o : itemList) {
					((BaseActivity) context).speak(VoiceCommandUtil.processChatResultString(o.toString()));
				}
			}
			if(context instanceof MainActivity)
				((BaseActivity) context).stopVoiceAlertDialog();
			return true;
		}

		if (ans != null) {
			ans = VoiceCommandUtil.processChatResultString(ans);
			((BaseActivity) context).speak(ans);
			return true;
		}
		return false;
	}

}
